{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 介绍\n",
    "这道题首先想到的是依次遍历，但是这样做会导致算法超时，因此需要进行优化。\n",
    "经过详细分析以后可以发现：\n",
    "对于一张图片而言，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "49\n"
     ]
    }
   ],
   "source": [
    "from typing import List, Tuple\n",
    "\n",
    "class Solution:\n",
    "    def maxArea(self, height: List[int]) -> int:\n",
    "        max_area = 0\n",
    "        # 双指针遍历\n",
    "        for i in range(len(height)):\n",
    "            for j in range(i+1, len(height)):\n",
    "                max_area = max(max_area, min(height[i], height[j]) * (j - i))\n",
    "        return max_area\n",
    "    \n",
    "if __name__ == \"__main__\":\n",
    "    height = [1,8,6,2,5,4,8,3,7]\n",
    "    print(Solution().maxArea(height))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "49\n"
     ]
    }
   ],
   "source": [
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def maxArea(self, height: List[int]) -> int:\n",
    "        l, r = 0, len(height) - 1\n",
    "        ans = 0\n",
    "        while l < r:\n",
    "            area = min(height[l], height[r]) * (r - l)\n",
    "            ans = max(ans, area)\n",
    "            # 当左边的高度小于右边的高度时，左指针右移，否则右指针左移\n",
    "            if height[l] <= height[r]:\n",
    "                l += 1\n",
    "            else:\n",
    "                r -= 1\n",
    "        return ans\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    height = [1,8,6,2,5,4,8,3,7]\n",
    "    print(Solution().maxArea(height))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pylearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
